// 命令枚举
use crate::core::Command;
use log::{error, info};
use std::io;
use tokio::sync::mpsc::Sender;
pub async fn cli_monitor(tx: Sender<Command>) -> Result<(), Box<dyn std::error::Error>> {
    let stdin = io::stdin();
    loop {
        let mut input = String::new();
        if stdin.read_line(&mut input).is_err() {
            Err("读取输入失败")?;
        }
        let input = input.trim();
        if input.is_empty() {
            continue;
        }
        if input == "list" || input == "ls" {
            tx.send(Command::ListDevices).await.unwrap();
        } else if input == "broadcast" || input == "b" {
            tx.send(Command::BroadcastDevices).await.unwrap();
        } else if input == "quit" || input == "q" {
            tx.send(Command::Quit).await.unwrap();
        } else if input == "clear" || input == "c" {
            tx.send(Command::ClearScreen).await.unwrap();
        } else if input == "remove" || input == "rm" {
            tx.send(Command::RemoveDevice(0)).await.unwrap();
        } else if input == "help" || input == "h" {
            tx.send(Command::Help).await.unwrap();
        } else if let Some((cmd, args)) = input.split_once(' ') {
            if cmd == "send" {
                if let Some((device_id, file_path)) = args.split_once(' ') {
                    tx.send(Command::SendFile(
                        device_id.parse::<u64>()?,
                        file_path.to_string(),
                    ))
                    .await
                    .unwrap();
                } else {
                    error!("格式错误!");
                }
            } else {
                info!("不稳定命令: {}", input);
            }
        } else {
            info!("未知命令: {}", input);
            info!("请输入help查看可用命令");
        }
    }
}
